﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Input;
using WickedFlame.Core;

namespace WickedFlame.Util.Controls
{
    public class HeaderedToolItem : ContentControl
    {
        static object syncRoot = new object();

        static HeaderedToolItem()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(HeaderedToolItem), new FrameworkPropertyMetadata(typeof(HeaderedToolItem)));
        }

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);

            this.CommandBindings.Add(new CommandBinding(ExpandCommand, this.OnExecuteCommand, this.OnCanExecuteCommand));
        }

        public DrawingCanvas DrawingCanvas
        {
            get
            {
                return (DrawingCanvas)GetValue(DrawingCanvasProperty);
            }
            set
            {
                SetValue(DrawingCanvasProperty, value);
            }
        }

        public static readonly DependencyProperty DrawingCanvasProperty =
            DependencyProperty.Register("DrawingCanvas", typeof(DrawingCanvas), typeof(HeaderedToolItem), new PropertyMetadata(new PropertyChangedCallback(OnLayerPropertyChanged)));

        private static void OnLayerPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
        }

        public string Description
        {
            get
            {
                return (string)GetValue(DescriptionProperty);
            }
            set
            {
                SetValue(DescriptionProperty, value);
            }
        }

        public static readonly DependencyProperty DescriptionProperty =
            DependencyProperty.Register("Description", typeof(string), typeof(HeaderedToolItem), new PropertyMetadata(null));

        public string Title
        {
            get
            {
                return (string)GetValue(TitleProperty);
            }
            set
            {
                SetValue(TitleProperty, value);
            }
        }

        public static readonly DependencyProperty TitleProperty =
            DependencyProperty.Register("Title", typeof(string), typeof(HeaderedToolItem), new PropertyMetadata(""));

        public object Icon
        {
            get { return (object)GetValue(IconProperty); }
            set { SetValue(IconProperty, value); }
        }

        public static readonly DependencyProperty IconProperty =
            DependencyProperty.Register("Icon", typeof(object), typeof(HeaderedToolItem),
            new FrameworkPropertyMetadata(null, new CoerceValueCallback(OnCoerce_Icon)));

        private static object OnCoerce_Icon(DependencyObject o, object value)
        {
            if (value is string)
            {
                Uri iconUri;
                // try to resolve given value as an absolute URI
                if (Uri.TryCreate(value as String, UriKind.RelativeOrAbsolute, out iconUri))
                {
                    ImageSource img = new BitmapImage(iconUri);
                    if (null != img)
                    {
                        Image icon = (o as HeaderedToolItem).Icon as Image;
                        if (null == icon)
                            icon = new Image();

                        icon.Source = img;
                        icon.Stretch = Stretch.None;
                        icon.SnapsToDevicePixels = true;

                        return icon;
                    }
                }
            }
            return value;
        }

        public bool IsExpanded
        {
            get
            {
                return (bool)GetValue(IsExpandedProperty);
            }
            set
            {
                SetValue(IsExpandedProperty, value);
            }
        }

        public static readonly DependencyProperty IsExpandedProperty =
            DependencyProperty.Register("IsExpanded", typeof(bool), typeof(HeaderedToolItem), new PropertyMetadata(false));

        private static RoutedUICommand expandCommand = null;
        public static RoutedUICommand ExpandCommand
        {
            get
            {
                lock (syncRoot)
                {
                    if (null == expandCommand)
                    {
                        expandCommand = new RoutedUICommand("ExpandCommand", "ExpandCommand", typeof(HeaderedToolItem));
                    }
                }
                return expandCommand;
            }
        }

        internal virtual void OnExecuteCommand(object sender, ExecutedRoutedEventArgs e)
        {
            if (!e.Handled && e.Command == ExpandCommand)
            {
                if (IsExpanded)
                    IsExpanded = false;
                else
                    IsExpanded = true;

                e.Handled = true;
            }
        }

        protected virtual void OnCanExecuteCommand(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }
    }
}
